<template>
	<view class="warranty-list-page">
		<view class="warranty-card" v-for="item in dataList" :key="item.repair_code">
			<view class="card-header">
				<view class="status-tag" :class="getStatusClass(item.repair_state_name)">
					{{ item.repair_state_name }}
				</view>
				<view class="bill-code">{{ item.repair_code }}</view>
				<view class="card-menu">···</view>
			</view>
			<view class="info-row">
				<text class="label">报修单名称</text>
				<text class="value">{{ item.repair_name }}</text>
			</view>
			<view class="info-row">
				<text class="label">报修人</text>
				<text class="value">{{ item.repair_user_name }}</text>
			</view>
			<view class="info-row">
				<text class="label">报修时间</text>
				<text class="value">{{ item.repair_date }}</text>
			</view>
			<view class="info-row">
				<text class="label">故障说明</text>
				<text class="value">{{ item.fault_description }}</text>
			</view>
<view class="action-row">
    <!-- 所有状态都有详情和审核记录 -->
    <up-button 
        type="primary"
        plain
        size="large"
        text="详情"
        @click="onDetail(item)"
        :customStyle="btnStyle"
    ></up-button>
    
    <up-button 
        type="primary"
        plain
        size="large"
        text="审核记录"
        @click="auditRecords(item)"
        :customStyle="btnStyle"
    ></up-button>

    <!-- 待审核或驳回状态：修改、删除 -->
    <template v-if="item.repair_state_name === '待审核' || item.repair_state_name === '驳回'">
        <up-button 
            type="warning"
            plain
            size="large"
            text="修改"
            @click="onEdit(item)"
            :customStyle="btnStyle"
        ></up-button>
        
        <up-button 
            type="error"
            plain
            size="large"
            text="删除"
            @click="onDelete(item)"
            :customStyle="btnStyle"
        ></up-button>
    </template>

    <!-- 待审核状态：审核 -->
    <up-button 
        v-if="item.repair_state_name === '待审核'"
        type="success"
        size="large"
        text="审核"
        @click="onAudit(item)"
        :customStyle="{...btnStyle, background: '#19be6b', color: '#fff'}"
    ></up-button>

    <!-- 审核通过状态：反审、指派 -->
    <template v-if="item.repair_state_name === '审核通过'">
        <up-button 
            type="warning"
            size="large"
            text="反审"
            @click="onUnAudit(item)"
            :customStyle="{...btnStyle, background: '#ff9900', color: '#fff'}"
        ></up-button>
        
        <up-button 
            type="info"
            size="large"
            text="指派"
            @click="onAssign(item)"
            :customStyle="{...btnStyle, background: '#909399', color: '#fff'}"
        ></up-button>
    </template>

    <!-- 维修完成状态：签收 -->
    <up-button 
        v-if="item.repair_state_name === '维修完成'"
        type="success"
        size="large"
        text="签收"
        @click="onFinish(item)"
        :customStyle="{...btnStyle, background: '#19be6b', color: '#fff'}"
    ></up-button>
</view>
		</view>
	</view>
</template>

<script setup>
	import {
		defineProps
	} from "vue";
	import http, {
		BASE_URL
	} from "@/api/request"; // 同时导入http和baseUrl

// 在 script 中定义的按钮样式
const btnStyle = {
    flex: 1,
    minWidth: '180rpx',  // 增大最小宽度
    height: '100rpx',    // 增大高度
    fontSize: '36rpx',   // 增大字体
    fontWeight: 'bold',  // 加粗字体
    margin: '0 10rpx',
    borderRadius: '20rpx', // 增加圆角
    borderWidth: '3rpx',  // 加粗边框
    letterSpacing: '1rpx' // 增加字间距提高可读性
}

	// 假设外部通过 props 传入 dataList
	const props = defineProps({
		dataList: {
			type: Array,
			default: () => []
		}
	});

	function getStatusClass(status) {
		if (status === '已完成') return 'status-done';
		if (status === '审核通过') return 'status-pass';
		if (status === '驳回') return 'status-reject';
		if (status === '待审核') return 'status-pending-review';
		if (status === '待维修') return 'status-pending-repair';
		if (status === '维修中') return 'status-repairing';
		if (status === '全部维修') return 'status-all-repair';
		if (status === '维修完成') return 'status-completed';
		return 'status-other';
	}

	function auditRecords(item) {
		uni.navigateTo({
			url: `/pages/assetWarranty/auditRecord?id=${item.id}`
		});
	}

	function onDetail(item) {
		uni.setStorageSync("assetWarrantyData", item);
		uni.navigateTo({
			url: '/pages/assetWarranty/detail?type=detail'
		});
	}

	function onEdit(item) {
		uni.setStorageSync("assetWarrantyData", item);
		uni.navigateTo({
			url: '/pages/assetWarranty/info?type=edit'
		});
	}

	function onAudit(item) {
		uni.setStorageSync("assetWarrantyData", item);
		// 审核逻辑，打开详情，展示通过驳回按钮和驳回原因
		uni.navigateTo({
			url: '/pages/assetWarranty/detail?type=audit'
		});
	}

	function onUnAudit(item) {
		// 反审逻辑
		uni.showModal({
			title: '反审确认',
			cancelText: '取消',
			confirmText: '确定',
			content: `确定反审报修单 ${item.repair_code} 吗？`,
			success: (res) => {
				if (res.confirm) {
					// 调用反审接口
					http.post("/api/module/Invoke", {
						"pageCode": "assetwarranty",
						"params": {
							"args": [{
									"name": "interfaceCode",
									"value": "Assetwarranty.updateStatus"
								},
								{
									"name": "repairMstr",
									"value": {
										"id": item.id,
										"repair_state": "PENDING_REVIEW"
									}
								}
							]
						},
						"methodName": "updateStatus"
					}).then((res) => {
						if (res.code && res.isSuccess) {
							uni.showToast({
								title: '反审成功',
								icon: 'success'
							});

							// 延迟一下再刷新页面，确保提示显示完成
							setTimeout(() => {
								// 使用全局事件通知父组件刷新数据
								uni.$emit('data-changed');
							}, 1000);
						} else {
							uni.showToast({
								title: '反审失败',
								icon: 'none'
							});
						}
					}).catch((err) => {
						console.error('反审失败：', err);
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						});
					});
				}
			}
		});
	}

	function onAssign(item) {
		// 指派逻辑
		uni.navigateTo({
			url: `/pages/assetWarranty/assign?id=${item.id}&repair_code=${item.repair_code}`
		});
	}

	function onFinish(item) {
		// 签收逻辑
		uni.showModal({
			title: '签收确认',
			cancelText: '取消',
			confirmText: '确定',
			content: `确定签收报修单 ${item.repair_code} 吗？`,
			success: (res) => {
				if (res.confirm) {
					// 调用签收接口
					http.post("/api/module/Invoke", {
						"pageCode": "assetwarranty",
						"params": {
							"args": [{
									"name": "interfaceCode",
									"value": "Assetwarranty.receipted"
								},
								{
									"name": "id",
									"value": item.id
								}
							]
						},
						"methodName": "receipted"
					}).then((res) => {
						if (res.code && res.isSuccess) {
							uni.showToast({
								title: '签收成功',
								icon: 'success'
							});
							// 延迟一下再刷新页面，确保提示显示完成
							setTimeout(() => {
								// 使用全局事件通知父组件刷新数据
								uni.$emit('data-changed');
							}, 1000);
						} else {
							uni.showToast({
								title: '签收失败',
								icon: 'none'
							});
						}
					}).catch((err) => {
						console.error('签收失败：', err);
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						});
					});
				}
			}
		});
	}

	function onDelete(item) {
		// 删除逻辑
		uni.showModal({
			title: '删除确认',
			cancelText: '取消',
			confirmText: '确定',
			content: `确定删除报修单 ${item.repair_code} 吗？`,
			success: (res) => {
				if (res.confirm) {
					// 调用删除接口
					http.post("/api/module/Invoke", {
						"pageCode": "assetwarranty",
						"params": {
							"args": [{
									"name": "interfaceCode",
									"value": "Assetwarranty.doLogicDeleteOne"
								},
								{
									"name": "id",
									"value": item.id
								}
							]
						},
						"methodName": "doLogicDeleteOne"
					}).then((res) => {
						if (res.code && res.isSuccess) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
							// 延迟一下再刷新页面，确保提示显示完成
							setTimeout(() => {
								// 使用全局事件通知父组件刷新数据
								uni.$emit('data-changed');
							}, 1000);
						} else {
							uni.showToast({
								title: '删除失败',
								icon: 'none'
							});
						}
					}).catch((err) => {
						console.error('删除失败：', err);
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						});
					});
				}
			}
		});
	}
</script>

<style scoped>
	.warranty-list-page {
		background: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx 0;
	}

	.warranty-card {
		background: #fff;
		border-radius: 16rpx;
		margin: 20rpx;
		padding: 24rpx 28rpx 18rpx 28rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
	}

	.card-header {
		display: flex;
		align-items: center;
		margin-bottom: 16rpx;
	}

	.status-tag {
		font-size: 24rpx;
		padding: 4rpx 16rpx;
		border-radius: 8rpx;
		margin-right: 16rpx;
		background: #e6f9f0;
		color: #2ecc71;
	}

	.status-done {
		background: #e6f9f0;
		color: #2ecc71;
	}

	.status-pass {
		background: #e6f7ff;
		color: #1890ff;
	}

	.status-reject {
		background: #fff2e8;
		color: #fa541c;
	}

	.status-pending-review {
		background: #f9f0ff;
		color: #722ed1;
	}

	.status-pending-repair {
		background: #fff7e6;
		color: #fa8c16;
	}

	.status-repairing {
		background: #e6fffb;
		color: #13c2c2;
	}

	.status-all-repair {
		background: #f6ffed;
		color: #52c41a;
	}

	.status-completed {
		background: #f6f6f6;
		color: #595959;
	}

	.status-other {
		background: #f5f5f5;
		color: #666;
	}

	.bill-code {
		font-weight: bold;
		font-size: 30rpx;
		flex: 1;
	}

	.card-menu {
		font-size: 36rpx;
		color: #bbb;
	}

	.info-row {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.label {
		color: #888;
		font-size: 26rpx;
		margin-right: 12rpx;
	}

	.value {
		color: #333;
		font-size: 28rpx;
		margin-right: 12rpx;
	}

	/* 操作按钮样式 */
	.action-row {
	    display: flex;
	    justify-content: space-between;
	    align-items: center;
	    flex-wrap: wrap;
	    margin-top: 30rpx;
	    padding-top: 30rpx;
	    border-top: 2rpx solid #f0f0f0;
	    gap: 20rpx;
	}
	
	/* 按钮点击反馈效果 */
	.up-button--active {
	    opacity: 0.8;
	    transform: scale(0.98);
	    transition: all 0.2s;
	}
</style>